<?php
if(!defined('IN_OPEN')) exit('no access');
if(!defined('OPEN_ROOT'))
{
	define('OPEN_ROOT',dirname(dirname(dirname(__FILE__))));
}
require_once OPEN_ROOT.'/lib/class/mysql.class.php';
class db
{
	private function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE, $dbcharset2 = '')
	{
		$this->db =  new mysql();
		$this->db->connect($dbhost, $dbuser, $dbpw, $dbname , $pconnect, $halt, $dbcharset2);
		$this->dbname = $dbname;
	}
	public static function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE, $dbcharset2 = '',$querycharset='utf8')
	{
		static $db = null;
		if($db==null)
		{
			$db = &new db($dbhost, $dbuser, $dbpw, $dbname , $pconnect, $halt, $dbcharset2,$querycharset);
			return $db;
		}
	}
	public function query($sql)
	{
		$query = $this->db->query($sql);
		return $query;
	}
	public  function fetchOne($sql)
	{
		$query = $this->db->query($sql);
		return $this->db->fetch_array($query);
	}
	public function fetchAll($sql,$key=null)
	{
		$res = $this->db->query($sql);
		if(false === $res) return false;
		$result = array();
		
			while($row = $this->db->fetch_array($res,MYSQL_ASSOC)){
				if($key) $result[$row[$key]]=$row;
				else $result[]=$row;
			}
		return $result;
	}
	public function checkdb()
	{
		if(empty($this->db)) $this->db = new mysql();
		
	}
} 
?>